package com.hnichr.ump.ping.service.impl;

import com.hnichr.ump.common.ping.entity.SmsSendContent;
import com.hnichr.ump.common.ping.service.SmsSendContentService;
import com.hnichr.ump.common.ping.vo.MobileResultVo;
import com.hnichr.ump.common.ping.vo.SendResultVo;
import com.hnichr.ump.common.ping.vo.SmsSendVo;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import com.mxpioframework.system.service.impl.BaseServiceImpl;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;


@Service
public class SmsSendContentServiceImpl extends BaseServiceImpl<SmsSendContent> implements SmsSendContentService {

    @Transactional
    @Override
    public void remove(String ids){
        String[] id = ids.split(",");
        Criteria criteria = Criteria.create().or();
        for(String key:id){
            SmsSendContent content = JpaUtil.getOne(SmsSendContent.class,key);
            if(content!=null){
                if(!StringUtils.equals(content.getStatus(),"-1")){
                    throw new RuntimeException("只有待发送状态的短信才能删除");
                }
                criteria.addCriterion("id", Operator.EQ,key);
            }
        }
        criteria = criteria.end();
        this.deleteBatch(SmsSendContent.class,criteria);
    }

    @Transactional
    @Override
    public void saveGroupSendResult(SmsSendVo smsSendVo,SendResultVo sendResultVo){
        if(sendResultVo==null||CollectionUtils.isEmpty(sendResultVo.getResult())){
            return;
        }
        for(MobileResultVo mobileVo:sendResultVo.getResult()){
            SmsSendContent smsSendContent = new SmsSendContent();
            smsSendContent.setMobile(mobileVo.getMobile());
            smsSendContent.setContent(smsSendVo.getSmsContent());
            smsSendContent.setTitle(smsSendVo.getTitle());
            smsSendContent.setStatus(mobileVo.getResultCode());
            smsSendContent.setSendTime(new Date());
            JpaUtil.save(smsSendContent);
        }
    }


}
